package com.appspot.diasporajava.federation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("XRD")
public class WebFingerVO {

	public static final String HDCARD = "http://microformats.org/profile/hcard";
	public static final String SEED_LOCATION = "http://joindiaspora.com/seed_location";
	public static final String GUID = "http://joindiaspora.com/guid";
	public static final String PROFILE_PAGE = "http://webfinger.net/rel/profile-page";
	public static final String UPDATES_FROM = "http://schemas.google.com/g/2010#updates-from";
	public static final String PUBLIC_KEY = "diaspora-public-key";

	@XStreamAlias("uri")
	@XStreamAsAttribute
	private String uri;

	@XStreamImplicit(itemFieldName = "Link")
	private List<Link> links;
	
	private Map<String,String> linkMap;

	@XStreamAlias("Subject")
	@XStreamAsAttribute
	private String subject;

	@XStreamAlias("Alias")
	private String alias;

	static class Link {
		@XStreamAlias("rel")
		@XStreamAsAttribute
		String rel;
		@XStreamAlias("type")
		@XStreamAsAttribute
		String type;
		@XStreamAlias("href")
		@XStreamAsAttribute
		String href;

		@Override
		public String toString() {
			return "Link [rel=" + rel + ", type=" + type + ", href=" + href
					+ "]";
		}

		public String getRel() {
			return rel;
		}

		public void setRel(String rel) {
			this.rel = rel;
		}

		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getHref() {
			return href;
		}

		public void setHref(String href) {
			this.href = href;
		}

	}

	@Override
	public String toString() {
		return "WebFingerVO [uri=" + uri + ", links=" + links + ", subject="
				+ subject + ", alias=" + alias + "]";
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public List<Link> getLinks() {
		return links;
	}

	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public void initLinkMap(){
		linkMap=new HashMap<String,String>();
		
		for(Link link:links){
			linkMap.put(link.rel, link.href);
		}
	}
	
	public String getLinkValue(String key){
		if(linkMap==null){
			initLinkMap();
		}
		return linkMap.get(key);
	}
	
}
